package com.ayu.shortlink.engine.mq.consumer;

import com.ayu.shortlink.common.cache.CaffeineCacheFactory;
import com.ayu.shortlink.common.cache.CommonCacheService;
import com.ayu.shortlink.engine.mq.basics.base.MessageWrapper;
import com.ayu.shortlink.engine.mq.basics.event.CouponDeleteEmptyCacheEvent;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import static com.ayu.shortlink.engine.common.constant.RocketMQConstant.GROUP_COUPON_DELETE_EMPTY_CACHE;
import static com.ayu.shortlink.engine.common.constant.RocketMQConstant.TOPIC_COUPON_DELETE_EMPTY_CACHE;

/**
 * 通知各engine服务器去删除空缓存消费者
 */
@Component
@RocketMQMessageListener(
        topic = TOPIC_COUPON_DELETE_EMPTY_CACHE,
        consumerGroup = GROUP_COUPON_DELETE_EMPTY_CACHE,
        messageModel = MessageModel.BROADCASTING
)
public class CouponDeleteEmptyCacheConsumer implements RocketMQListener<MessageWrapper<CouponDeleteEmptyCacheEvent>> {
    @Override
    public void onMessage(MessageWrapper<CouponDeleteEmptyCacheEvent> couponDeleteEmptyCacheEventMessageWrapper) {
        CouponDeleteEmptyCacheEvent message = couponDeleteEmptyCacheEventMessageWrapper.getMessage();

        // 删除空库存本地缓存
        CommonCacheService<String, String> couponNotStock = CaffeineCacheFactory.getCouponNotStock();
        couponNotStock.remove(message.getShopNumber()+"_"+message.getCouponTemplateId());
    }
}
